package search.service.analysis;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import search.config.SearchParameters;
import common.dao.elastic.ElasticSearchDao;

/**
 * Created by julianzliu on 2016/7/3.
 */
public class QueryCombinator {

    public static BoolQueryBuilder combineBaseAndExpansionForMatchQuery(ElasticSearchDao esDao, String text,String field,
                                                                        String base_minimum,String expo_minimum,
                                                                        String base_analyzer, String expo_analyzer)
    {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        BoolQueryBuilder base_query  = QueryExpansion.queryWithMatchQuery(text,field,base_minimum,base_analyzer);
        BoolQueryBuilder expan_query = QueryBuilders.boolQuery();
        try {
            expan_query = QueryExpansion.queryExpansionWithDictForMatchQuery(esDao, text,field,expo_minimum,expo_analyzer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        query.should(
                base_query.boost(SearchParameters.Boosting.original_query)
        );
        query.should(
                expan_query.boost(SearchParameters.Boosting.expasion_query)
        );
        return query;
    }



    public static BoolQueryBuilder combineBaseAndExpansionForMultiMatchQuery(ElasticSearchDao esDao, String text,String[] fields,
                                                                             String base_minimum,String expo_minimum,
                                                                             String base_analyzer, String expo_analyzer)
    {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        BoolQueryBuilder base_query  = QueryExpansion.queryWithMultiMatchQuery(text,fields,base_minimum,base_analyzer);
        BoolQueryBuilder expan_query = QueryBuilders.boolQuery();
        try {
            expan_query = QueryExpansion.queryExpansionWithDictForMultiMatchQuery(esDao, text,fields,expo_minimum,expo_analyzer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        query.should(
                base_query.boost(SearchParameters.Boosting.original_query)
        );
        query.should(
                expan_query.boost(SearchParameters.Boosting.expasion_query)
        );
        return query;
    }
}
